/*
 * Copyright (c) 2010, okazoh_tk. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  - Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  - Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *  - Neither the name of the copyright holder nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef ALKES_WIN32TEXT_WIN32_GLYPH_PROVIDER_H_INCLUDED_
#define ALKES_WIN32TEXT_WIN32_GLYPH_PROVIDER_H_INCLUDED_

#include "alkes/win32text/internal.h"
#include "alkes/text/IGlyphProvider.h"
#include "alkes/core/TExtendBuffer.h"

namespace alkes {

class AL_WTX_EXPORT Win32GlyphProvider
    : public IGlyphProvider
{
    AL_DECLARE_CLASS(Win32GlyphProvider, IGlyphProvider);

public:
    static Win32GlyphProvider* create(HWND hwnd, LOGFONT& font);

protected:
    virtual bool onGetGlyphMetrics(int32_t ch, GlyphMetrics& metrics);
    virtual bool onRenderChar(int32_t ch, RenderBuffer& target, GlyphMetrics& metrics);

    virtual bool lock(ImageBuffer& dst, uint16_t x, uint16_t y, RenderBuffer& target);
    virtual void unlock(ImageBuffer& dst);

private:
    enum Win32Flag
    {
        FLAG_USE_SYSTEM_FONT    = 0,
        FLAG_RELEASE_HDC        = 1,
        FLAG_RELEASE_FONT       = 2,
    };

    Win32GlyphProvider(HWND hwnd, HFONT font);
    virtual ~Win32GlyphProvider();

    virtual void destroy();

    bool getTEXTMETRIC(TEXTMETRIC& metrics);
    void* getBuffer(uint32_t request_size);
    void convertGlyphMatrix(const GLYPHMETRICS& src, GlyphMetrics& dst) const;
    static void copyBuffer(RenderBuffer& target, const void* buffer, const GlyphMetrics& metrics);
    bool setFont();
    void restoreFont();

private:
    HWND hwnd_;
    HDC hdc_;
    HFONT font_;
    HGDIOBJ backup_;
    TExtendBuffer<> buffer_;
};

}

#endif
